package utils

import (
	"github.com/golang-jwt/jwt"
	"ruoyi-hello/common"
	"ruoyi-hello/config"
	"strings"
	"time"
)

func CreateToken(UserName string, UserId int, DeptId int) (string, error) {
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
		"user_name": UserName,
		"user_id":   UserId,
		"dept_id":   DeptId,
		"exp":       time.Now().Unix() + int64(config.Config.Jwt.JwtTtl),
		"iss":       "Ruoyi-hello",
	})

	mySigningKey := []byte(config.Config.Jwt.Secret)

	return token.SignedString(mySigningKey)
}

func VerifyToken(tokenStr string) (*jwt.Token, error) {
	mySigningKey := []byte(config.Config.Jwt.Secret)
	tokenStr = strings.ReplaceAll(tokenStr, common.HeaderSignTokenStr, "")
	return jwt.Parse(tokenStr, func(t *jwt.Token) (interface{}, error) {
		return mySigningKey, nil
	})
}
